﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using System.Data.SqlClient;
using System.Collections;

namespace Jxym.wlkc2011.Module.Default.SERVER.role
{
    public partial class MenuTreeAllSave : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string returnValue = "{success:false,msg:'参数不正确'}";
            try
            {
                int roleid = 0; if (Request.Params["roleid"] != null) { roleid = Convert.ToInt32(Request.Params["roleid"]); }

                string pvs = ""; if (Request.Params["pvs"] != null) { pvs = Request.Params["pvs"]; }

                if (roleid != 0)
                {
                    string[] pvsList = pvs.Split(',');

                    MsSqlDal<User_RolePermissionConfig> iUrpc = new MsSqlDal<User_RolePermissionConfig>();

                    if (iUrpc.IsExist("RoleID=" + roleid, null))
                    {
                        iUrpc.Delete("RoleID=" + roleid, null);
                    }

                    if (pvs != "")
                    {
                        int count = 0;
                        foreach (string pv in pvsList)
                        {
                            string[] plist = pv.Split(':');

                            string menuId = plist[0];

                            string pvValue = SAUDtoPV(plist[1]);

                            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@RoleID", roleid), new SqlParameter("@MenuID", menuId), new SqlParameter("@PermissionValue", pvValue) };
                            iUrpc.Add(parameters);
                            count++;
                        }
                        returnValue = count.ToString();
                    }

                }
                Response.Write("{success:true,msg:'保存成功'}");
            }
            catch
            {
                Response.Write(returnValue);
            }
        }

        private string SAUDtoPV(string command)
        {
            string validation = "";
            char[] comms = command.ToCharArray();
            char[] pvs = new char[] { '0', '0', '0', '0' };

            foreach (char c in comms)
            {
                pvs[0] = '1'; //查
                if (c == 'A') pvs[1] = '1'; //增
                if (c == 'U') pvs[2] = '1'; //改
                if (c == 'D') pvs[3] = '1'; //删
            }

            foreach (char c in pvs)
            {
                validation += c.ToString();
            }

            return validation;
        }
    }
}